<?php

namespace App\Http\Controllers\Admin\Collection;

use App\Http\Controllers\Controller;
use App\Models\Admin\Collection\CollectionCompany;
use App\Models\Admin\Collection\CollectionResume;
use App\Models\Admin\Collection\CollectionResumeCertificate;
use App\Models\Admin\Collection\CollectionResumeCheck;
use App\Models\Admin\Collection\CollectionResumeEducation;
use App\Models\Admin\Collection\CollectionResumeLanguageSkill;
use App\Models\Admin\Collection\CollectionResumeOther;
use App\Models\Admin\Collection\CollectionResumeProjectExp;
use App\Models\Admin\Collection\CollectionResumeSkill;
use App\Models\Admin\Collection\CollectionResumeWorkExp;
use App\Models\Admin\Collection\CollectionTrackCompany;
use App\Models\Admin\Collection\CollectionTrackResume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DataBase extends Controller{
    //简历板块---------------------------------------------------------------------------------------------------------
    /**
     * 查询数据库是不是已经存在简历
     * @param $resumeId //跟踪简历唯一ID
     * @param $webMain //跟踪的所属网站
     * @return bool
     */
    static public function checkResume($resumeId, $webMain){
        $result = CollectionTrackResume::where('web_id', $resumeId)->where('web_main', $webMain)->first();
        if(!empty($result)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 已经在临时数据库中的话 插入采集简历更新跟踪表
     * @param $resumeId
     * @param $resumeUrl
     * @return array
     */
    static public function addResumeCheck($resumeId, $resumeUrl, $webMain, $page){
        $collectionTrackResume = CollectionTrackResume::where('web_id', $resumeId)->where('web_main', $webMain)->where('real_resume_id', '!=', null)->first(['id', 'real_resume_id']);
        if(!empty($collectionTrackResume)){
            //是不是已经插入
            $collectionResumeCheck = CollectionResumeCheck::where('resume_id', $collectionTrackResume->real_resume_id)->first(['id']);
            if(empty($collectionResumeCheck)){
                $data['resume_id'] = $collectionTrackResume->real_resume_id;
                $data['source'] = $resumeUrl;
                //每页减去20分
                $minutes = ($page-1) * 20;
                $ptime = date('Y-m-d H:i:s', strtotime('-' . $minutes . ' minutes'));
                $data['created_at'] = $ptime;
                //插入真实简历ID到采集简历更新跟踪表
                CollectionResumeCheck::create($data);
            }
        }
//        if(empty($collectionResumeCheck)){
//            $data['resume_id'] = $collectionTrackResume->real_resume_id;
//            $data['source'] = $resumeUrl;
//            $data['created_at'] = date('Y-m-d H:i:s', time());
//            CollectionResumeCheck::create($data);
//            //插入采集简历更新跟踪表
//            if(CollectionResumeCheck::insertGetId($data)){
//                return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . '插入简历' . $resumeId . '到更新跟踪表成功！');
//            }else{
//                return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 2, 'msg' => date('Y-m-d H:i:s', time()) . '插入简历' . $resumeId . '到更新跟踪表失败！');
//            }
//        }else{
//            return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 3, 'msg' => '当前简历' . $resumeId . '已经在更新跟踪表存在！');
//        }
    }


    /**
     * 已经在数据库中的话 直接更新数据库里面的时间为当前时间
     * @param $resumeId //跟踪简历唯一ID
     * @param $webMain //跟踪的所属网站
     */
    static public function updateResume($resumeId, $resumeUrl, $webMain, $cookie, $collArea){
        $data = DB::table('collection_track_resume as t1')
            ->join('collection_resume as t2', 't1.resume_id', '=', 't2.id')
            ->select('t2.updatetime', 't2.id', 't1.source')
            ->where('t1.web_id', $resumeId)
            ->where('t1.web_main', $webMain)
            ->first();
        if(!empty($data)){
            $fifteen = date('Y-m-d H:i:s', strtotime("-15 days"));//十五天内
            $ninety = date('Y-m-d H:i:s', strtotime("-90 days"));//90天内
            //超过15天并小于90内
            if(($ninety < $data->updatetime) && ($data->updatetime < $fifteen)){
                //更新简历时间
                $result = CollectionResume::where('id', $data->id)->update(['show_type' => 1, 'updatetime' => date('Y-m-d H:i:s', time())]);
                if($result){
                    return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 刷新简历' . $resumeId . '成功！');
                }else{
                    return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 2, 'msg' => '刷新简历' . $resumeId . '失败！');
                }
            }elseif($data->updatetime < $ninety){
                //采集简历数据
                $arrayCon = '';
                switch($webMain){
                    case 'tzzp' :
                        $upMobile = '1';//为1代表90天后对这份简历不更新联系方式（手机号码、邮箱、qq）
                        $arrayCon = Tzzp::resumeCon($resumeId, $resumeUrl, $cookie, $collArea, $upMobile);
                        break;
                    case '58' :
                        $arrayCon = FiveEight::resumeCon($resumeId, $resumeUrl, $cookie, $collArea);
                        break;
                }
                //更新简历数据到临时表
                self::updateResumeData($arrayCon, $data->id);
            }else{
                return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 3, 'msg' => '当前简历' . $resumeId . '已经存在！');
            }
        }else{
            //更新采集跟踪简历数据信息表
            $collectionTrackResume = CollectionTrackResume::where('web_id', $resumeId)->where('web_main', $webMain)->where('real_resume_id', '!=', null)->first(['id', 'real_resume_id']);
            if(!empty($collectionTrackResume)){
                CollectionTrackResume::where('id', $collectionTrackResume->id)->update(['source' => $resumeUrl]);
            }else{
                return array('resumeId' => $resumeId, 'resumeUrl' => $resumeUrl, 'status' => 3, 'msg' => '当前简历' . $resumeId . '已经不存在！');
            }
        }
    }

    /**
     * 更新临时采集数据
     * @param $arrayCon
     * @param $webMain
     */
    static public function updateResumeData($arrayCon, $resumeId){
        //更新基本信息表
        $result = CollectionResume::where('id', $resumeId)->update($arrayCon['basic']);
        if($result){
            CollectionResumeWorkExp::where('resume_id', $resumeId)->delete();
            CollectionResumeProjectExp::where('resume_id', $resumeId)->delete();
            CollectionResumeEducation::where('resume_id', $resumeId)->delete();
            CollectionResumeLanguageSkill::where('resume_id', $resumeId)->delete();
            CollectionResumeSkill::where('resume_id', $resumeId)->delete();
            CollectionResumeCertificate::where('resume_id', $resumeId)->delete();
            CollectionResumeOther::where('resume_id', $resumeId)->delete();
            //工作经验
            if(count($arrayCon['work']) > 0){
                foreach($arrayCon['work'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeWorkExp::create($val);
                }
            }
            //项目经验
            if(count($arrayCon['project']) > 0){
                foreach($arrayCon['project'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeProjectExp::create($val);
                }
            }
            //教育经历
            if(count($arrayCon['education']) > 0){
                foreach($arrayCon['education'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeEducation::create($val);
                }
            }
            //语言能力
            if(count($arrayCon['language']) > 0){
                foreach($arrayCon['language'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeLanguageSkill::create($val);
                }
            }
            //技能专长
            if(count($arrayCon['skill']) > 0){
                foreach($arrayCon['skill'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeSkill::create($val);
                }
            }
            //获得证书
            if(count($arrayCon['certificate']) > 0){
                foreach($arrayCon['certificate'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeCertificate::create($val);
                }
            }
            //其他信息
            if(count($arrayCon['other']) > 0){
                foreach($arrayCon['other'] as $val){
                    $val['resume_id'] = $resumeId;
                    CollectionResumeOther::create($val);
                }
            }
            return array('resumeId' => $resumeId, 'resumeUrl' => $arrayCon['resumeUrl'], 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 更新简历' . $resumeId . '数据成功！');
        }else{
            return array('resumeId' => $resumeId, 'resumeUrl' => $arrayCon['resumeUrl'], 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 更新简历' . $resumeId . '数据失败！');
        }
    }

    /**
     * 插入临时简历数据
     * @param $arrayCon //简历信息内容
     * @param $webMain //所属网站
     */
    static public function insertResume($arrayCon, $webMain, $page){
        //每页减去20分
        $minutes = ($page-1) * 20;
        $ptime = date('Y-m-d H:i:s', strtotime('-' . $minutes . ' minutes'));
        $arrayCon['basic']['regtime'] = $ptime;
        //插入简历
        if($id = CollectionResume::insertGetId($arrayCon['basic'])){
            //工作经验
            if(count($arrayCon['work']) > 0){
                foreach($arrayCon['work'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeWorkExp::create($val);
                }
            }
            //教育经历
            if(count($arrayCon['education']) > 0){
                foreach($arrayCon['education'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeEducation::create($val);
                }
            }
            //项目经验
            if(count($arrayCon['project']) > 0){
                foreach($arrayCon['project'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeProjectExp::create($val);
                }
            }
            //语言能力
            if(count($arrayCon['language']) > 0){
                foreach($arrayCon['language'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeLanguageSkill::create($val);
                }
            }
            //技能专长
            if(count($arrayCon['skill']) > 0){
                foreach($arrayCon['skill'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeSkill::create($val);
                }
            }
            //获得证书
            if(count($arrayCon['certificate']) > 0){
                foreach($arrayCon['certificate'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeCertificate::create($val);
                }
            }
            //其他信息
            if(count($arrayCon['other']) > 0){
                foreach($arrayCon['other'] as $val){
                    $val['resume_id'] = $id;
                    CollectionResumeOther::create($val);
                }
            }
            //简历采集跟踪器
            $CollectionTrack['resume_id'] = $id;
            $CollectionTrack['web_id'] = $arrayCon['resumeId'];
            $CollectionTrack['web_main'] = $webMain;
            $CollectionTrack['source'] = $arrayCon['resumeUrl'];
            CollectionTrackResume::create($CollectionTrack);
            return array('resumeId' => $arrayCon['resumeId'], 'resumeUrl' => $arrayCon['resumeUrl'],
                'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 插入一条新简历' . $arrayCon['resumeId'] . '成功！');
        }else{
            return array('resumeId' => $arrayCon['resumeId'], 'resumeUrl' => $arrayCon['resumeUrl'],
                'status' => 2, 'msg' => date('Y-m-d H:i:s', time()) . ' 插入新简历' . $arrayCon['resumeId'] . '失败！');
        }
    }

    //企业板块---------------------------------------------------------------------------------------------------------
    /**
     * 查询数据库是不是已经存在企业
     * @param $resumeId //跟踪企业唯一ID
     * @param $webMain //跟踪的所属网站
     * @return bool
     */
    static public function checkCompany($companyId, $webMain){
        $result = CollectionTrackCompany::where('web_id', $companyId)->where('web_main', $webMain)->first();
        if(!empty($result)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 插入临时企业数据
     * @param $arrayCon //企业信息内容
     * @param $webMain //所属网站
     */
    static public function insertCompany($arrayCon, $webMain){
        //插入简历
        if($id = CollectionCompany::insertGetId($arrayCon['companyArr'])){
            //简历采集跟踪器
            $CollectionTrackCompany['company_id'] = $id;
            $CollectionTrackCompany['web_id'] = $arrayCon['companyId'];
            $CollectionTrackCompany['web_main'] = $webMain;
            $CollectionTrackCompany['source'] = $arrayCon['companyUrl'];
            CollectionTrackCompany::create($CollectionTrackCompany);
            return array('companyId' => $arrayCon['companyId'], 'companyUrl' => $arrayCon['companyUrl'],
                'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 插入一条新企业' . $arrayCon['companyId'] . '成功！');
        }else{
            return array('companyId' => $arrayCon['companyId'], 'companyUrl' => $arrayCon['companyUrl'],
                'status' => 2, 'msg' => date('Y-m-d H:i:s', time()) . ' 插入新企业' . $arrayCon['companyId'] . '失败！');
        }
    }

    /**
     * 已经在数据库中的话 直接更新数据库里面的时间为当前时间
     * @param $resumeId //跟踪简历唯一ID
     * @param $webMain //跟踪的所属网站
     */
    static public function updateCompany($companyId, $companyUrl, $webMain, $cookie){
        $data = DB::table('collection_track_company as t1')
            ->join('collection_company as t2', 't1.company_id', '=', 't2.id')
            ->select('t2.updated_at', 't2.id', 't1.source', 't1.company_id')
            ->where('t1.web_id', $companyId)
            ->where('t1.web_main', $webMain)
            ->first();
        if(!empty($data)){
            $fifteen = date('Y-m-d H:i:s', strtotime("-15 days"));//十五天内
            $ninety = date('Y-m-d H:i:s', strtotime("-90 days"));//90天内
            if($data->updated_at < $fifteen){
                //更新企业时间
                $result = CollectionCompany::where('id', $data->id)->update(['updated_at' => date('Y-m-d H:i:s', time())]);
                if($result){
                    return array('companyId' => $companyId, 'companyUrl' => $companyUrl, 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 刷新企业' . $companyId . '成功！');
                }else{
                    return array('companyId' => $companyId, 'companyUrl' => $companyUrl, 'status' => 2, 'msg' => '刷新企业' . $companyId . '失败！');
                }
            }elseif($data->updated_at < $ninety){
                //采集企业数据
                $arrayCon = '';
                switch($webMain){
                    case 'tzzp' :
                        $arrayCon = Tzzp::companyCon($companyId, $companyUrl, $cookie);
                        break;
                    case '58' :
                        $arrayCon = FiveEight::companyCon($companyId, $companyUrl, $cookie);
                        break;
                }
                if($arrayCon == 'err'){
                    return array('companyId' => $companyId, 'companyUrl' => $companyUrl, 'status' => 3, 'msg' => '该企业' . $companyId . '已经不存在！');
                }

                //更新企业数据到临时表
                $result = CollectionCompany::where('id', $data->id)->update($arrayCon['basic']);//更新基本信息表
                if($result){
                    return array('companyId' => $arrayCon['companyId'], 'companyUrl' => $arrayCon['companyUrl'], 'status' => 1, 'msg' => date('Y-m-d H:i:s', time()) . ' 更新企业' . $arrayCon['companyId'] . '数据成功！');
                }else{
                    return array('companyId' => $arrayCon['companyId'], 'companyUrl' => $arrayCon['companyUrl'], 'status' => 2, 'msg' => '更新企业' . $arrayCon['companyId'] . '数据失败！');
                }
            }else{
                return array('companyId' => $companyId, 'companyUrl' => $companyUrl, 'status' => 3, 'msg' => '该企业' . $companyId . '已经存在！');
            }
        }else{
            return array('companyId' => $companyId, 'companyUrl' => $companyUrl, 'status' => 3, 'msg' => '该企业' . $companyId . '已经不存在！');
        }
    }
}
